/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import DTO.Object.Registers.CheckRegisterType;
import DTO.Object.Registers.CheckRegisters;
import Model.DBUtil;
import Model.XMLvalidateXSD;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

/**
 *
 * @author Kim
 */
public class registerServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        String button = request.getParameter("btRegister");
        if (button.equals("Register")) {
            try {
                boolean flagregister=false;
                String url = "index.jsp";
                Statement stmt = null;
                
                Connection con = null;
                String realpath = getServletContext().getRealPath("/");
                String address = request.getParameter("txtAddress");
                String Pnumber = request.getParameter("txtPhone");
                String mail = request.getParameter("txtMail");
                String fname = request.getParameter("txtFullname");
                String pass = request.getParameter("txtPassword");
                String name = request.getParameter("txtUsername");
                CheckRegisters checkRegisters = new CheckRegisters();
                CheckRegisterType checkRegisterType = new CheckRegisterType();
                checkRegisterType.setUsername(name);
                checkRegisterType.setPassword(pass);
                checkRegisterType.setFullname(fname);
                checkRegisterType.setEmail(mail);
                checkRegisterType.setPhone(Pnumber);
                checkRegisterType.setAddress(address);
                checkRegisters.getCheckRegister().add(checkRegisterType);
                JAXBContext jaxb = JAXBContext.newInstance(checkRegisters.getClass());
                Marshaller mar = jaxb.createMarshaller();
                mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
                mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
                mar.marshal(checkRegisters, new File(realpath + "XML/checkRegister.xml"));
                boolean validateregister = XMLvalidateXSD.XMLvalidate(realpath + "XML/checkRegister.xml", realpath + "schema/checkRegister.xsd");
                if (validateregister) {
                    try {
                        //insert database
                        DBUtil dbutil = new DBUtil();

                        String sql = "";
                        Class.forName(dbutil.getDriverName());
                        //String url = "jdbc:sqlserver://ANNT60258-PC:1433;databaseName=eShopping";
                        con = DriverManager.getConnection(dbutil.getURL(), dbutil.getUsername(), dbutil.getPassword());
                        stmt = con.createStatement();
                        sql = ("insert into UserAccount(UserName,Password,FullName,Mail,Phone,Address,isActive) values('" + name + "','" + pass + "','" + fname + "','" + mail + "','" + Pnumber + "','" + address + "',1)");
                        stmt.execute(sql);
                        con.close();
                        stmt.close();
                        flagregister=true;
                        
                        //ket thuc insert
                    } catch (SQLException ex) {
                        Logger.getLogger(registerServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(registerServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                if(flagregister) session.setAttribute("Login", "Successful");
                else session.setAttribute("Login", "Fail");
                RequestDispatcher rqd = request.getRequestDispatcher(url);
                rqd.forward(request, response);

            } catch (JAXBException ex) {
                Logger.getLogger(registerServlet.class.getName()).log(Level.SEVERE, null, ex);
            }



        }

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
